﻿@{
    ViewBag.Title = "Manage Schedule - Project Management System";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Manage Schedule</h2>
<form action="" method="post" id="form" name="formSchedule">
<input type="hidden" id="url" name="url" />
<input type="hidden" id="Action" name="Action" />

<ul class="shortcut-buttons-set" >
    @{if (PMS.Utilities.Utility.RoleID == 2)
      {
        <li><a class="shortcut-button" href="@Url.Action("AddSchedule", "Schedule")"><span>
            <img src="../../Content/images/icons/Button-Add-icon.png" alt="icon" /><br />
            Add</span></a></li>
        <li><a class="shortcut-button" href="#" onclick="if(DoAction('@Url.Action("EditSchedule", "Schedule")')==-1)jAlert('Please Select One Item', 'Message');">
            <span>
                <img src="../../Content/images/icons/paper_content_pencil_48.png" alt="icon" /><br />
                Edit</span></a></li>
        <li><a class="shortcut-button" href="javascript:Action('Delete')"><span>
            <img src="../../Content/images/icons/Button-Close-icon.png" alt="icon" /><br />
            Delete</span></a></li>
        <li><a class="shortcut-button" href="javascript:Action('Confirm')"><span>
            <img src="../../Content/images/icons/Actions-dialog-ok-apply-icon.png" alt="icon" /><br />
            Confirm</span></a></li>
        <li><a class="shortcut-button" href="#" id="btnGroup"><span>
            <img src="../../Content/images/icons/Actions-user-group-new-icon.png" alt="icon" /><br />
            Add to Group</span></a></li>
        <li><a class="shortcut-button" href="#" id="btnUnGroup"><span>
            <img src="../../Content/images/icons/Actions-user-group-delete-icon.png" alt="icon" /><br />
            Remove Group</span></a></li>
        <li><a class="shortcut-button" href="javascript:Action('Print')"><span>
            <img src="../../Content/images/icons/Hardware-Printer-Green-icon.png" alt="icon" /><br />
            Print</span></a></li>
      }
    }
    <li><a class="shortcut-button" href="#" id="btnTimeLine" onclick="if(DoAction('@Url.Action("TimeLine", "Schedule")')==-1)jAlert('Please Select One Item', 'Message');">
        <span>
            <img src="../../Content/images/icons/Actions-view-calendar-timeline-icon.png" alt="icon" /><br />
            Timeline</span></a></li>
</ul>

<!-- End .shortcut-buttons-set -->
<div class="clear">
</div>
<!-- End .clear -->
<!-- Search -->
<input type="hidden" id="isSearch" name="isSearch" value="@ViewData["IsSearch"]" />
<div class="content-box" id="Search">
    <!-- Start Content Box -->
    <div class="content-box-header">
        <h3 id="toggleSearch">
            Search</h3>
        <div class="clear">
        </div>
    </div>
    <!-- End .content-box-header -->
    <div class="content-box-content">
        <fieldset>
            <p>
                <label class="width12 floatLeft">
                    Schedule Name:</label>
                <input type="text" id="txtScheduleName" name="ScheduleName" class="width20 input-inline text-input medium-input" value="@ViewData["ScheduleName"]"/>
            </p>
            <div class="childCol">
                <p>
                    <label class="width12 floatLeft">
                        Project:</label>
                    @Html.DropDownList("ProjectID", (SelectList)ViewData["ProjectList"], new { @class = "width20 input-inline", onchange = "$('#Page').attr('value', 1);$('form').submit();" })
                </p>
                <p>
                    <label class="width12 floatLeft">
                        Semester:</label>
                    @Html.DropDownList("SemesterID", (SelectList)ViewData["SemesterList"], new { @class = "width20 input-inline", onchange = "$('#Page').attr('value', 1);$('form').submit();" })
                </p>
            </div>
            <div class="childCol">
                <p>
                    <label class="width12 floatLeft">
                        Owner:</label>
                    @Html.DropDownList("UserID", (SelectList)ViewData["UserList"], new { @class = "width20 input-inline", onchange = "$('#Page').attr('value', 1);$('form').submit();" })
                </p>
                <p>
                    <label class="width12 floatLeft">
                        Status:</label>
                    <select class="width20 input-inline" id="cmbStatus" name="Status" onchange="$('#Page').attr('value', 1);$('form').submit();">
                        <option value="--All--">--All--</option>
                        <option value="Draft">Draft</option>
                        <option value="Confirm">Confirm</option>
                    </select>
                </p>
            </div>
        </fieldset>
    </div>
</div>
<div class="content-box">
    <!-- Start Content Box -->
    <div class="content-box-header">
        <h3>
            Schedule List</h3>
        <div class="clear">
        </div>
    </div>
    <!-- End .content-box-header -->
    <div class="content-box-content">
        <div>
            <!-- This is the target div. id must match the href of this div's tab -->
            @{ if (TempData["Message"] != null)
               {
                <div class="notification attention png_bg">
                    <a href="#" class="close">
                        <img src="../../Content/images/icons/cross_grey_small.png" title="Close this notification"
                            alt="close" /></a>
                    <div>
                        @TempData["Message"].ToString()
                    </div>
                </div>
               }
            }
            @{ 
                var pageSize = int.Parse(ViewData[PMS.ENT.PageInfo.PageSize.ToString()].ToString());
                var pageNum = int.Parse(ViewData[PMS.ENT.PageInfo.PageNum.ToString()].ToString());
                var page = int.Parse(ViewData[PMS.ENT.PageInfo.Page.ToString()].ToString()); }
            <input type="hidden" value="@ViewData["OrderBy"]" name="orderBy" id="orderBy" />
            <input type="hidden" value="@ViewData["OrderDirection"]" name="orderDirection"
                id="orderDirection" />
            <input type="hidden" name="GroupID" id="GroupID" />
            <input type="hidden" id="CurUserID" name="CurUserID" value="@PMS.Utilities.Utility.UserID" />
            @{if (Model.Count <= 0)
              {
                <div class="RecordFound @("RecordFound" + @Model.Count)">
                    No Record Found</div>
              }
            }
            <table class="flexigird TableGrid">
                <thead>
                    <tr>
                        <th id="Selected">
                            <input class="check-all" type="checkbox" id="checkAll" />
                        </th>
                        <th id="ScheduleName">
                            Schedule Name
                        </th>
                        <th id="Project">
                            Project
                        </th>
                        <th id="Owner">
                            Owner
                        </th>
                        <th id="Semester">
                            Semester
                        </th>
                        <th id="StartDate">
                            StartDate
                        </th>
                        <th id="EndDate">
                            EndDate
                        </th>
                        <th id="Groups">
                            Group
                        </th>
                        <th id="Status">
                            Status
                        </th>
                    </tr>
                </thead>
                <tbody>
                    @{ for (int i = 0; i < Model.Count; i++)
                       {
                           string scheduleName = @Model[i].ScheduleName;
                           string projectName = @Model[i].ProjectName;
                           string group = @Model[i].Groups.ToString();
                        <tr>
                            <td>
                                <input type="checkbox" id="ScheduleID" name="ScheduleID" value="@Model[i].ScheduleID"
                                        group="Selected" onclick="Uncheck('ScheduleID');" status="@Model[i].Status" owner="@Model[i].OwnerID" groups="@Model[i].Groups" />
                            </td>
                            <td>
                                @Html.ActionLink(scheduleName, "ViewSchedule", new { ID = Model[i].ScheduleID }, null)
                            </td>
                            <td>
                                @Html.ActionLink(projectName, "ViewProject", "Project", new { ID = Model[i].ProjectID }, null)
                            </td>
                            <td>
                                @Model[i].Owner
                            </td>
                            <td>
                                @Model[i].SemesterName
                            </td>
                            <td>
                                @Model[i].StartDate.ToString("dd/MM/yyyy")
                            </td>
                            <td>
                                @Model[i].EndDate.ToString("dd/MM/yyyy")
                            </td>
                            <td style="text-align: center">
                                @Html.ActionLink(group, "ManageGroup", "Project", new { ScheduleID = Model[i].ScheduleID }, new { id = "link" + Model[i].ScheduleID })
                            </td>
                            <td>
                                @Html.Encode(Model[i].Status)
                            </td>
                        </tr>
                       } 
                    }
                </tbody>
            </table>
            @{Html.RenderPartial("Pagination", null, ViewData); }
        </div>
        <!-- End #tab1 -->
    </div>
    <!-- End .content-box-content -->
</div>
<!-- End .content-box -->
</form>
<script language="javascript" type="text/javascript">
    $("#url").val(document.URL);
    $("#MSchedule").addClass("current");
    $("#Schedule").addClass("current");
    $("#toggleSearch").click(function () {
        if ($("#isSearch").val() == 1) {
            $("#isSearch").val(0);
        }
        else {
            $("#isSearch").val(1);
        }
    });
    $('#txtScheduleName').keydown(function (event) {
        if (event.keyCode == 13) {
            $('form').submit();
        }
    });
    var sstatus = @Html.Raw(Json.Encode(ViewData["Status"]));
    $("#cmbStatus").val(sstatus);

    var isSearch = @ViewData["IsSearch"];
    if (isSearch == '1') {
        $("#Search").removeClass('closed-box');
    }
    else {
        $("#Search").addClass('closed-box');
    }

    $("#btnGroup").click(function () {
        var checked = $("input[group='Selected']:checked").length;
        if (checked > 1 || checked == 0)
            jAlert('Please, select one item!', 'Message');
        else {
            var status = $("input[group='Selected']:checked").attr('status');
            if (status == '@PMS.ENT.ProjectStatus.Confirm.ToString()') {
                $.cookie('SelectedItem', $("input[group='Selected']:checked").val());
                jQuery.facebox(' <h3>\
                                Add schedule to group</h3>\
                                <fieldset>\
                                    <p>\
                                        <label class="width12 floatLeft">\
                                            Group:</label>\
                                        <select class="width20 input-inline" id="cmbGroup" >\
                                            <option></option>\
                                        </select>\
                                    </p>\
                                    <input class="button" type="button" value="Add" id="btnJoin" onclick="Join();" />\
                                </fieldset>'
                            );
                var ScheduleID = $("input[group='Selected']:checked").val();
                $.post('@Url.Action("GetGroupByScheduleID", "Schedule")', { ID: ScheduleID }, function (data) {
                    if (data != null) {
                        //Invoice To
                        var options = $("#cmbGroup").attr('options');
                        $("#cmbGroup").children().remove();
                        $.each(data.ListGroup, function (index, Subject) {
                            var option = new Option(Subject.GroupName, Subject.GroupID);
                            options[options.length] = option;
                        });
                    }
                });
            }
            else if (status == '@PMS.ENT.ProjectStatus.Draft.ToString()') {
                jAlert('Please, select confirmed project!', 'Message');
            }
            else if (status == '@PMS.ENT.ProjectStatus.Start.ToString()') {
                jAlert('Project has been started!', 'Message');
            }
        }

    });

    $("#btnUnGroup").click(function () {
        var checked = $("input[group='Selected']:checked").length;
        if (checked > 1 || checked == 0)
            jAlert('Please, select one item!', 'Message');
        else {
            var status = $("input[group='Selected']:checked").attr('status');
            if (status == '@PMS.ENT.ProjectStatus.Confirm.ToString()') {
                var curUserID = $("#CurUserID").val();
                var ownerID = $("input[group='Selected']:checked").attr('owner');
                if (curUserID != ownerID) {
                    jAlert("Schedule is not yours", "Error");
                    return;
                }
                $.cookie('SelectedItem', $("input[group='Selected']:checked").val());
                jQuery.facebox(' <h3>\
                                Remove group from schedule</h3>\
                                <fieldset >\
                                <div id="listGroup" ></div>\
                                </fieldset>'
                            );
                var ScheduleID = $("input[group='Selected']:checked").val();
                $.post('@Url.Action("GetJoinGroupByScheduleID", "Schedule")', { ID: ScheduleID }, function (data) {
                    if (data != null) {
                        $.each(data.ListGroup, function (index, Group) {
                            var body = '<p id="pGroup' + Group.GroupID + '" style=" width: 300px;  word-wrap: break-word;   ">\
                                    ' + Group.GroupName;

                            body = body + ' <a href="#" onclick="RemoveGroup(' + ScheduleID + ',' + Group.GroupID + ')"> &nbsp;&nbsp;&nbsp;&nbsp; \
                                <img src="../../Content/images/cancel_48.gif" alt="" /></a>';

                            body = body + '</p>';

                            $("#listGroup").append(body);

                        });
                    }
                });
            }
            else if (status == '@PMS.ENT.ProjectStatus.Draft.ToString()') {
                jAlert('Please, select confirmed project!', 'Message');
            }
        }

    });

    function Join() {
        var val = $("#cmbGroup").val();
        $("#Action").val('Schedule');
        $("#GroupID").val(val);
        $('form').submit();
    };

  

    function RemoveGroup(id, groupID) {
        jConfirm('Are you sure to remove this group?', 'Warning', function (r) {
            if (r) {
                $.post('@Url.Action("RemoveFromGroup", "Schedule")', { ID: id, GroupID: groupID }, function (data) {
                    if (data != null) {
                        if (data.Result == 'False') {
                            jAlert(data.Message, 'Message');
                        }
                        else {
                            var groups = $("input[group='Selected']:checked").attr('groups');
                            groups = groups - 1;
                            $("input[group='Selected']:checked").attr('groups', groups);

                            $("#link" + id).text(groups);
                            $("#pGroup" + groupID).remove();
                        }
                    }
                });

            }
        });
    }
  
</script>
